package com.flying.pas.security.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;
import com.flying.pas.security.model.Org;

@Service(value="OrgService", desc="")
public class OrgService extends AbstractService{
	private UserService us;
	public void setUserService(UserService s) {
		this.us = s;
	}
        
	@MethodInfo("新增")
	@DaoCreate(entity=Org.ENTITY_NAME)
	public Org create(
			@Param(value=Org.PARENT_ID, required=false,	desc="上级机构ID") Long parent_id,
			@Param(value=Org.CODE, required=true,	desc="机构代码") String code,
			@Param(value=Org.NAME, required=true,	desc="机构名称") String name,
			@Param(value=Org.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=Org.IS_LEAF, required=false,	desc="是否页节点（0：否；1：是）") Integer is_leaf,
			@Param(value=Org.CREATOR, required=false,	desc="创建人") String creator,
			@Param(value=Org.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		
		) throws Exception {
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(Org.class,
				Org.CREATOR, p == null?null: p.getName(),
				Org.CREATE_TIME, new Date());
	}

	@MethodInfo("修改")
	@Transaction
	@DaoUpdate(entity=Org.ENTITY_NAME)
	public ActionResult update(
			@Param(value=Org.ORG_ID, required=true,	desc="机构ID") Long org_id,
			@Param(value=Org.PARENT_ID, required=false,	desc="上级机构ID") Long parent_id,
			@Param(value=Org.CODE, required=true,	desc="机构代码") String code,
			@Param(value=Org.NAME, required=true,	desc="机构名称") String name,
			@Param(value=Org.REMARKS, required=false,	desc="说明") String remarks,
			@Param(value=Org.IS_LEAF, required=false,	desc="是否页节点（0：否；1：是）") Integer is_leaf,
			@Param(value=Org.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=Org.UPDATE_TIME, required=false,	desc="修改时间") Date update_time
		) throws Exception {
		this.us.updateOrgCode(org_id, code);
		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(ActionResult.class,
				Org.UPDATOR, p == null?null: p.getName(),
				Org.UPDATE_TIME, new Date());
	}

	@MethodInfo("删除")
	@DaoRemove(entity=Org.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=Org.ORG_ID, required=true,	desc="机构ID") Long org_id
		) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=Org.ENTITY_NAME, pageable=true, modelClass=Org.class)
	public QueryResult<Org> findByParentId(
			@Param(value=Org.PARENT_ID, required=false,	desc="上级机构ID") Long parent_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=Org.ENTITY_NAME, pageable=true, modelClass=Org.class)
	public QueryResult<Org> findAll(
			@Param(value=Org.PARENT_ID, required=false,	desc="上级机构ID") Long parent_id,
			@Param(value=Org.CODE, required=false,	desc="机构代码") String code,
			@Param(value=Org.NAME, required=false,	desc="机构名称", tag="LIKE") String name,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Org.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public Org findByCode(
			@Param(value=Org.CODE, required=true,	desc="机构代码") String code
		) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Org.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public Org findById(
			@Param(value=Org.ORG_ID, required=true,	desc="机构ID") Long org_id
		) throws Exception {
		return null;
	}

}